// pages/address/address.js
let utils = require("../../utils/utils.js");
const app = getApp();
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		username: '请输入绑定姓名',
		phone: "请输入绑定手机号码",
		gaddress: "请输入公司地址",
		gname: "请输入公司名称",
		email: '2665644567@qq.com',
		birthday: '请选择出生日期',
		telephonenumber: '29292929',
		isDisabled: false,
		isComplete: false,
		buttonTitle: "完成",
		date: '2016-09-01',
		getUserInfo:'getUserInfo',
		k7: [{
				'arr_guige02': '男',
				'guige_key02': 0,
				'checked': true
			},
			{
				'arr_guige02': '女',
				'guige_key02': 1,
				'checked': false
			},
		],
		sex: '男',
		userInfo: {}
		/* personalinfo:{
			username:'钟海',
			phone:'18126495953',
			gaddress:'全伟达工业园201',
			gname:'旮旯云商有限公司',
			birthday:'1989.11.12',
			sex:'女'
		} */
		
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let that=this;
		let userInfo = wx.getStorageSync('userInfo');
		console.log(userInfo);
		//if(userInfo.)
		
		
		
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
	
	},
	
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		let that=this;
		that.getuserinfo();
	},
	getuserinfo(e){
		let that=this;
		let userInfo=wx.getStorageSync('userInfo');
		let k7=that.data.k7;
		
		let url='/user/get?id=' + userInfo.userId
		utils.request(userInfo,url,'get','',res=>{
			console.log(res);
			if(res.data.status==200){
				let personalinfo=res.data.data;
				for(let i=0;i<k7.length;i++){
					if(k7[i].arr_guige02==personalinfo.sex){
						k7[i].checked=true;
					}
				}
				that.setData({
					username:personalinfo.username,
					birthday:personalinfo.birthday,
					phone:personalinfo.phoneNumber,
					gname:personalinfo.companyName,
					gaddress:personalinfo.companyAdress,
					k7:k7,
				})
			}else{
				
				utils._login(res=>{
					console.log(res);
				})
			}
			
		})
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {
	
	},
	
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
	
	},
	
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
	
	},
	
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
	
	},
	
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
	
	},
	radio: function(e) {
		this.setData({
			guige_key02: e.currentTarget.dataset.id
		})
	},
	// 发货地址选择,获取用户选择的单选框的值
	radioChange: function(e) {
		this.data.sex = e.detail.value;
	},
	bindDateChange(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value);
		this.data.birthday=e.detail.value;
		this.setData({
			birthday: e.detail.value
		})
	},
	formSubmit(e) {
		let that=this;
	    let userInfo=wx.getStorageSync('userInfo');
		let addrInfo={
			username:e.detail.value.username,
			phone:e.detail.value.phone,
			birthday:'',
			sex:'',
			gname:e.detail.value.gname,
			gaddress:e.detail.value.gaddress
		};
		addrInfo.sex = this.data.sex;
		addrInfo.birthday = this.data.birthday;
		if(userInfo.avatarUrl==''){
			wx.showModal({
				title: "提示",
				content: "请授权获取个人信息",
				showCancel: false
			});
		}else if (addrInfo.username == "" || addrInfo.phone == "" || addrInfo.birthday == "请选择出生日期" || addrInfo.gname == "" || addrInfo.gaddress ==
			"") {
			this.data.isComplete = false;
			wx.showModal({
				title: "提示",
				content: "请完善信息",
				showCancel: false
			});
		} else if (!/^[1][3,4,5,7,8]\d{9}$/.test(addrInfo.phone)) {
			wx.showModal({
				title: "提示",
				content: "手机号格式不规范",
				showCancel: false
			});
		} else {
			this.data.isComplete = true;
			wx.setStorageSync('addrInfo', addrInfo);
			that.updateAddr();
			return false;
		}
		this.setData({
			isDisabled: true,
			isComplete: this.data.isComplete
			// buttonTitle: "修改"
		});
	},
	/* 提交编辑 */
	updateAddr(e) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		let addrInfo = wx.getStorageSync('addrInfo');
		let base_url = app.globalData.baseurl;
		let url = '/base/edit';
		let data={
			'id':userInfo.userId,
			'username': addrInfo.username,
			'sex': addrInfo.sex,
			'birthday': addrInfo.birthday,
			'phoneNumber': addrInfo.phone,
			'companyName': addrInfo.gname,
			'companyAdress': addrInfo.gaddress
		}
		console.log(data);
		//return false;
		utils.request(userInfo,url, 'post', data,res=>{
			console.log(res)
			if(res.data.status==200){
				console.log(res.data);
			}else if(res.data.status==500){
				
				
			}else{
				utils.login(res=>{
					console.log(res);
					let jsonuserinfo = res.data.data;
					if (jsonuserinfo.requireBind != true) {
						userInfo.openId = jsonuserinfo.openId;
						userInfo.headerValue = jsonuserinfo.sessionValue;
						userInfo.userId = jsonuserinfo.userId;
						wx.setStorageSync('userInfo', userInfo);
						that.updateAddr();
					}
				});
			}
			return false;
		})
		
	}
	
})
